import store from "@/store/index.js"
// import {debounce} from "@/utils/debounceThrottle.js"
const timedelay = 500;
/**
 * @description  跳转通用延迟模式
 * @param {type} path 路径
 * @param {type} type 跳转类型
 * @param {type} params 参数
 * */
export function toPage(path, type, params) {
	let pages = getCurrentPages();
	if (pages && pages.length > 0) {
		store.commit("router/setRouter", pages.map(
			item => {
				return item.route
			}
		))
	}
	let timeout = null;
	// if (getApp().globalData.flag) {
		// getApp().globalData.flag = false
		switch (type) {
			// 关闭当前页面跳转
			case 'redirectTo':
				uni.redirectTo({
					url: params ? path + makeQuery(params) : path
				})
				timeout = setTimeout(
					() => {
						getApp().globalData.flag = true;
						clearTimeout(timeout)
					},
					timedelay
				);
				break;
				// 关闭所有跳转	
			case 'reLaunch':
				uni.reLaunch({
					url: params ? path + makeQuery(params) : path
				})
				timeout = setTimeout(
					() => {
						getApp().globalData.flag = true;
						clearTimeout(timeout)
					},
					timedelay
				);
				break;
				// 回跳tab页面	
			case 'switchTab':
				uni.switchTab({
					url: params ? path + makeQuery(params) : path,
				})
				timeout = setTimeout(
					() => {
						getApp().globalData.flag = true;
						clearTimeout(timeout)
					},
					timedelay
				);
				break;
			default:
				uni.navigateTo({
					url: params ? path + makeQuery(params) : path
				})
				timeout = setTimeout(
					() => {
						getApp().globalData.flag = true;
						clearTimeout(timeout)
					},
					timedelay
				);
				break;
		}
	}

/**
 * @description  obj转换url请求
 * @param {type} queryObject 路径
 * */
export function makeQuery(queryObject) {
	const query = Object.entries(queryObject)
		.reduce((result, entry) => {
			result.push(entry.join('='))
			return result
		}, []).join('&')
	return `?${query}`
}

/**
 * 时间戳格式转换
 * */
export function formatToDate(date = 0, fmt = 'yyyy-MM-dd') {
	date = parseInt(date)
	if (!date) {
		return false;
	}
	date = new Date(+date)
	if (/(y+)/.test(fmt)) {
		fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
	}
	let o = {
		'Y+': date.getYear(),
		'M+': date.getMonth() + 1,
		'd+': date.getDate()
	};
	for (let k in o) {
		if (new RegExp(`(${k})`).test(fmt)) {
			let str = o[k] + '';
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : ('00' + str).substr(str.length));
		}
	}
	return fmt;
}

// export function toPage(path, type, params){
// 	debounce(toPage1(path, type, params),10000)
// }
